package uform.model.generator;

/**
 * ...
 * @author Franco Ponticelli
 */

 import haxe.macro.Expr;

@:macro class Generator 
{
	public static function generate(t : haxe.macro.Expr) 
	{
		var alternative = {
		   expr : ECall({
			   expr : EField({
					expr : EType({
					   expr : EField({
						   expr : EField({
							   expr : EConst(CIdent("uform")),
							   pos : t.pos 
						   },"model"), 
						   pos : t.pos
					   },"generator"),
					   pos : t.pos },"RuntimeGenerator"),
				   pos : t.pos },"generate"),
			   pos : t.pos },
			   [t]),
		   pos : t.pos
		};
		switch(t.expr)
		{
			case EConst(c):
				switch(c)
				{
					case CType(s):
						var type = haxe.macro.Context.getType(s);
						var extractor = new uform.model.generator.MacroExtractor();
						var definitions = extractor.extract(type);
						var generator = new uform.model.generator.MacroSchemaGenerator(definitions);
						var generated = generator.toString();
						return haxe.macro.Context.parse(generated, t.pos);
					default:
						return alternative;
				}
			default:
				return alternative;
		}
		

	}
}